package com.foxzzz.graph;


import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class Solution990 {
    public boolean equationsPossible(String[] equations) {
        Set<Set<String>> sets = new HashSet<>();
        List<String[]> list = new LinkedList<>();

        for (String equation : equations) {

            String a = "";
            String b = "";
            Boolean equals = null;
            for (int i = 0; i < equation.length(); i++) {
                char x = equation.charAt(i);
                if (x == '!') {
                    equals = false;
                    i++;
                } else if (x == '=') {
                    equals = true;
                    i++;
                } else {
                    if (equals == null) {
                        a += x;
                    } else {
                        b += x;
                    }
                }
            }
            if (a.equals(b)) {
                if (equals) {
                    continue;
                } else {
                    return false;
                }
            }
            if (equals) {
                Set<String> aSet = null;
                Set<String> bSet = null;
                for (Set<String> set : sets) {
                    if (set.contains(a)) {
                        aSet = set;
                    }
                    if (set.contains(b)) {
                        bSet = set;
                    }
                }
                if (aSet == null && bSet == null) {
                    Set<String> set = new HashSet<>();
                    set.add(a);
                    set.add(b);
                    sets.add(set);
                } else if (aSet != null && bSet != null && aSet != bSet) {
                    aSet.addAll(bSet);
                    sets.remove(bSet);
                } else if (aSet == null) {
                    bSet.add(a);
                } else {
                    aSet.add(b);
                }
            } else {
                list.add(new String[]{a, b});
            }
        }
        for (String[] strings : list) {
            for (Set<String> set : sets) {
                if (set.contains(strings[0]) && set.contains(strings[1])) {
                    return false;
                }
            }
        }

        return true;
    }


    public static void main(String[] args) {
        Solution990 solution990 = new Solution990();
        System.out.println(solution990.equationsPossible(new String[]{"a==b", "b!=a"})); //false
        System.out.println(solution990.equationsPossible(new String[]{"b==a", "a==b"})); //true
        System.out.println(solution990.equationsPossible(new String[]{"a==b", "b==c", "a==c"})); //true
        System.out.println(solution990.equationsPossible(new String[]{"a==b", "b!=c", "c==a"}));//false
        System.out.println(solution990.equationsPossible(new String[]{"c==c", "b==d", "x!=z"}));//true

    }
}
